package com.neusoft.neuiotms.dcom.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.InspectionTaskDTO;
import com.neusoft.neuiotms.dcom.service.InspectionTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "巡检任务")
@RestController
@RequestMapping(value = "/api/w/inspection/task")
@Slf4j
public class InspectionTaskController {
    @Autowired
    private InspectionTaskService taskService;

    @ApiOperation("查询所有巡检任务")
    @GetMapping
    public ResultListDTO<InspectionTaskDTO> index(final Searchable searchable) {
        return ResultListDTO.success(this.taskService.index(searchable));
    }

    @ApiOperation("分页查询巡检任务")
    @GetMapping("/p")
    public PageResultDTO<InspectionTaskDTO> page(final Searchable searchable, final Pageable pageable,
            final UserBean user) {
        return PageResultDTO.success(this.taskService.page(searchable, pageable, user));
    }

    @ApiOperation("更新巡检任务")
    @PutMapping("/{id}")
    public ResultDTO<InspectionTaskDTO> update(@PathVariable final Long id, @RequestBody final InspectionTaskDTO dto) {
        return ResultDTO.success(this.taskService.update(id, dto));
    }

    @ApiOperation("提交巡检任务")
    @PutMapping("/submit/{id}")
    public ResultDTO<InspectionTaskDTO> update(@PathVariable final Long id, @RequestBody final InspectionTaskDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.taskService.submit(id, dto, user));
    }

    @ApiOperation("查看巡检任务")
    @GetMapping("/{id}")
    public ResultDTO<InspectionTaskDTO> detail(@PathVariable final Long id) {
        return ResultDTO.success(this.taskService.detail(id));
    }

    @ApiOperation("领取巡检任务")
    @PutMapping("/claim/{id}")
    public ResultDTO<Void> claim(@PathVariable final Long id, final UserBean user) {
        this.taskService.claim(id, user);
        return ResultDTO.success();
    }

    @ApiOperation("导出巡检结果")
    @GetMapping(value = "/export")
    public void download(final Searchable searchable, final HttpServletResponse response) {

        final Workbook workbook = this.taskService.download(searchable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("巡检结果" + LocalDateTime.now() + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            InspectionTaskController.log.error("巡检导出失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                InspectionTaskController.log.error("巡检导出关闭流失败", e);
            }
        }
    }

}
